package com.zyq.ssyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyq.ssyx.acl.service.AdminRoleService;
import com.zyq.ssyx.acl.service.AdminService;
import com.zyq.ssyx.acl.service.RoleService;
import com.zyq.ssyx.common.result.CommonResponse;
import com.zyq.ssyx.model.acl.Admin;
import com.zyq.ssyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author clap of thunder
 * @date 2023-11-27 11:06
 * @description
 */

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private RoleService roleService;

    /*
    获取后台用户分页列表(带搜索)
    */
    @ApiOperation(value = "获取管理用户分页列表")
    @GetMapping("{page}/{limit}")
    public CommonResponse getPageList(
            @ApiParam(name = "page", value = "查询的当前页数", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "分页查询每页容量", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "adminQueryVo", value = "模糊查询like条件", required = false)
                    AdminQueryVo adminQueryVo) {//入参VO是为了接值用作后续的模糊查询

        Page<Admin> adminParam = new Page<Admin>(page, limit);
        IPage<Admin> result = adminService.selectPage(adminParam, adminQueryVo);
        return CommonResponse.ok(result);
    }



    /*
    根据ID获取某个后台用户
    */
    @ApiOperation("根据ID获取某个后台用户")
    @GetMapping("/{id}")
    public CommonResponse getById(
            @ApiParam(name = "id",value ="用户ID" ,required = true)
            @PathVariable Long id){
        Admin admin = adminService.getById(id);
        return CommonResponse.ok(admin);

    }

    /*
    保存一个新的后台用户
    */
    @ApiOperation("新增一个新的后台用户")
    @PostMapping("/save")
    public CommonResponse add(
            @ApiParam(name = "admin",value ="待保存用户实体" ,required = true)
            @RequestBody Admin admin){
        //调用MD5加密
        return CommonResponse.ok(adminService.safeSave(admin));
    }

    /*
    更新一个后台用户
    */
    @ApiOperation("更新一个后台用户")
    @PutMapping("/update")
    public CommonResponse update(
            @ApiParam(name = "admin",value ="待保存用户实体" ,required = true)
            @RequestBody Admin admin){
        adminService.updateById(admin);
        return CommonResponse.ok();

    }

    /*
    删除某个用户
    */
    @ApiOperation("删除某个用户")
    @DeleteMapping("/{id}")
    public CommonResponse remove(
            @ApiParam(name = "id",value ="id" ,required = true)
            @PathVariable Long id){
        adminService.removeById(id);
        return CommonResponse.ok();
    }

    /*
    批量删除多个用户
    ids的结构: ids是包含n个id的数组
    */
    @ApiOperation("批量删除多个用户")
    @DeleteMapping("/batchRemove")
    public CommonResponse batchRemove(
            @ApiParam(name = "ids",value ="多个ID" ,required = true)
            @RequestBody List<Long> ids){
        adminService.removeByIds(ids);
        return CommonResponse.ok();
    }

    /*获取所有角色，
    以及根据ID查已经对该用户已分配好的角色
    结果是角色，故而写在roleService里*/
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{adminId}")
    public CommonResponse toAssign(@PathVariable Long adminId){

        //获取角色ID已分配的所有角色列表
        Map<String,Object> map = roleService.getRolesByAdminId(adminId);
        return CommonResponse.ok(map);
    }




}
